#！/bin/bash

getBranch(){
	curbranch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p');
	result=$(echo $curbranch | grep "dev");
	if [ "$result" != "" ]
	then 
		echo "dev";
	else
		echo "reg";
	fi
}

getMaxTag(){
	tags=$1;
	prefix=$2;
	for index in {1..99}
	do
		tagIndex=${index};
		if [ ${index} -lt 10 ];then
			tagIndex="0${index}";
		fi
		result=`echo ${tags} | grep "${prefix}_${tagIndex}"`
		if [ "$result" == "" ];then
			echo ${prefix}"_"${tagIndex};
			return;
		fi
	done
}

echo "检查本地是否有修改"
changedFiles=`git status -s`;
#echo -e $changedFiles ;
if [ changedFiles -eq "" ]
then
	echo "本地无修改记录，不提交，只更新，不进行下一步"；
	git pull;
else 
	echo "本地有修改记录，先提交，再更新"
	#msg=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p');
	git add .
	git commit -m "当前分支"; #当前分支名
	git pull;
	if [ $? -ne 0 ]
	then
		echo "本地有冲突，请先解决冲突";
		exit;
	else
		echo "请确认是否push? y or n";
		read cf;
		if [ $cf = y ]
		then
			#set -x;
			echo "开始push";# tag dev_date_num dev_20020510_03//reg_20020503_05
			echo "获取当前分支";
			branch=`getBranch`;
			echo $branch;
			newDate=`ls -l ./release | tail -n 1 | awk '{print $9}'`;
			datestr=${newDate%.*};#`echo ${newDate} | tr ".txt" ""`;
			str="$branch"_"$datestr";
			echo $str;
			tagList=`git tag -l | grep "${str}"`;
			echo $tagList;
			newTag=`getMaxTag "${tagList}" "${str}"`;
			echo $newTag;
			git tag ${newTag};
			git push origin ${newTag};
			#set +x;
			exit;
		else
			echo "先不push";
			exit;
		fi
	fi
fi